package com.hariro.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.qianfeng.project.R;

public class Fragment_ShiYi_Adapter extends BaseAdapter {
	private List<Map<String, Object>> list;
	private Context context;
	private Display display;
	private LayoutInflater inflater = null;
	private DisplayImageOptions options = null;
	private ImageLoader imageLoader = null;

	public Fragment_ShiYi_Adapter(Context context,
			List<Map<String, Object>> list, Display display) {
		this.context = context;
		this.list = list;
		this.display = display;

		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		options = new DisplayImageOptions.Builder()
				// .showImageOnLoading(R.drawable.common_loading_refresh)
				.showImageForEmptyUri(R.drawable.default_ptr_rotate)
				.showImageOnFail(R.drawable.activity_again_request_icon)
				.cacheInMemory(true).cacheOnDisc(true).considerExifParams(true)
				.displayer(new RoundedBitmapDisplayer(5)).build();

		imageLoader = ImageLoader.getInstance();
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder mHolder;
		if (convertView == null) {
			mHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_gridview_shiyi, parent, false);
			mHolder.textView_item_shiyi_title = (TextView) convertView
					.findViewById(R.id.textView_item_shiyi_title);
			mHolder.imageView_item_shiyi = (ImageView) convertView
					.findViewById(R.id.imageView_item_shiyi);
			// 贴标签
			convertView.setTag(mHolder);
		} else {
			mHolder = (ViewHolder) convertView.getTag();
		}

		mHolder.textView_item_shiyi_title.setText(list.get(position)
				.get("title").toString());

		// 从list中获取图片的url
		String imgUrl = "";
		if (list.get(position).get("shareImgUrl").toString().equals("")) {
			imgUrl = list.get(position).get("iconUrl").toString();
		} else {
			imgUrl = list.get(position).get("shareImgUrl").toString();
		}

		Log.i("--->图片url: ", imgUrl);
		// 获取展示界面的宽度
		int width_all = display.getWidth();
		// 修改图片视图的布局
		LayoutParams params = mHolder.imageView_item_shiyi.getLayoutParams();
		params.width = (width_all - 5) / 2;
		params.height = params.width;
		mHolder.imageView_item_shiyi.setLayoutParams(params);
		mHolder.imageView_item_shiyi.setScaleType(ScaleType.FIT_XY);
		try {
			// 用UIL异步加载网络图片并缓存到本地
			imageLoader.displayImage(imgUrl, mHolder.imageView_item_shiyi,
					options);
		} catch (IllegalStateException e) {
			Toast.makeText(context, "程序员GG开了个小差...", Toast.LENGTH_SHORT).show();
		}
		return convertView;
	}

	class ViewHolder {
		private TextView textView_item_shiyi_title;
		private ImageView imageView_item_shiyi;
	}

	// 新增刷新网格布局的方法
	// public void reloadGridView(){
	// notifyDataSetChanged();
	// };
}
